<?php
declare (strict_types=1);

namespace app\controller;

use app\BaseController;
use app\common\UserService;
use app\model\Room;
use app\model\RoomFiles;
use app\model\RoomLog;
use DateTime;
use think\db\Query;
use think\facade\Log;

class RoomController extends BaseController
{
	function index()
	{
		$sort = $this->request->get('sort/d'); #排序:1综合排序 2最新发布 3结拍时间由近到远 4价格升序 5价格倒序
		$district_id = $this->request->get('district_id/d'); #所属区 0全部
		$property = $this->request->get('property/d'); #物业类型:1=住宅用房,2=工业用房,3=商业用房,4=其他用房
		$price = $this->request->get('price'); #价格:1 100万以下 2 100-200万 3 200-300万 4 300-500万 5 500-1000万 6 1000万以上 7自定义
		$minprice = $this->request->get('minprice');
		$maxprice = $this->request->get('maxprice');
		$status = $this->request->get('status/a',''); #状态:1=正在进行,2=即将开始,3=已结束,4=中止,5=撤回
		$stage = $this->request->get('stage/a',''); #拍卖阶段:1=一拍,2=二拍,3=重新拍卖,4=变卖
		$space = $this->request->get('space/d'); #面积:1=1-50m²,2=50-100m²,3=100-200m²,4=200-300m²,5=300-500m²,6=500m²以上,7=自定义
		$minspace = $this->request->get('minspace/d');
		$maxspace = $this->request->get('maxspace/d');
		$type = $this->request->get('type/d'); #类型:1=司法拍卖,2=资产拍卖,3=捡漏
		$keyword = $this->request->get('keyword');
	
		$rows = Room::field('*, ABS(endtime - UNIX_TIMESTAMP(NOW())) AS time_diff')
		
			->when(!empty($district_id), function (Query $query) use ($district_id) {
				$query->where('district_id', $district_id);
			})
			->when(!empty($keyword),function (Query $query)use($keyword){
				$query->whereLike('name', '%'.$keyword.'%');
			})
			->when(!empty($type), function (Query $query) use ($type) {
				if ($type == 1 || $type == 2) {
					$query->where('type', $type);
				}
				if ($type == 3) {
					$query->where('status', 1)->where('enroll_count', '<', 3);
				}
			})
			->when(!empty($property), function (Query $query) use ($property) {
				$query->where('property', $property);
			})
			->when(!empty($price), function (Query $query) use ($price, $minprice, $maxprice) {
				if ($price == 1) {
					$query->whereBetween('nowprice', '0,1000000');
				}
				if ($price == 2) {
					$query->whereBetween('nowprice', '1000000,2000000');
				}
				if ($price == 3) {
					$query->whereBetween('nowprice', '2000000,3000000');
				}
				if ($price == 4) {
					$query->whereBetween('nowprice', '3000000,5000000');
				}
				if ($price == 5) {
					$query->whereBetween('nowprice', '5000000,10000000');
				}
				if ($price == 6) {
					$query->where('nowprice', '>=', '10000000');
				}
				if ($price == 7) {
					$query->whereBetween('nowprice', "$minprice,$maxprice");
				}
			})
			->filter(function ($data){
				$data-> nowprice = round($data-> nowprice/10000, 2);
				$data-> startprice = round($data-> startprice/10000,2);
			})
			->when(!empty($status), function (Query $query) use ($status) {
				$query->whereIn('status', $status);
			})
			->when(!empty($sort), function (Query $query) use ($sort) {
				if ($sort == 1) {
					$query->order(['status'=>'asc','id'=>'desc']);
				}
				if ($sort == 2) {
					$query->order(['createtime' => 'desc']);
				}
				if ($sort == 3) {
					$query->order(['status'=>'asc','time_diff' => 'asc']);
				}
				if ($sort == 4) {
					$query->order(['nowprice' => 'asc']);
				}
				if ($sort == 5) {
					$query->order(['nowprice' => 'desc']);
				}
			})
			->when(!empty($stage), function (Query $query) use ($stage) {
				$query->whereIn('stage', $stage);
			})
			->when(!empty($space), function (Query $query) use ($space, $minspace, $maxspace) {
				if ($space == 1) {
					$query->whereBetween('space', '0,50');
				}
				if ($space == 2) {
					$query->whereBetween('space', '50,100');
				}
				if ($space == 3) {
					$query->whereBetween('space', '100,200');
				}
				if ($space == 4) {
					$query->whereBetween('space', '200,300');
				}
				if ($space == 5) {
					$query->whereBetween('space', '300,500');
				}
				if ($space == 6) {
					$query->where('space', '>=', '500');
				}
				if ($space == 7) {
					$query->whereBetween('space', "$minspace,$maxspace");
				}
			})
			->paginate()
			->items();
		$this->writeJson($rows);
	}
	
	#详情
	function detail()
	{
		$id = $this->request->get('id/d');
		$row = Room::with(['user_collect' => function (Query $query) {
			$query->where(['user_id' => $this->user_id]);
		}])->withCount('user_collect')->filter(function ($data){
			$data->nowprice = number_format((float)$data->nowprice);
		})->findOrFail($id);
		if ($row->origin == 2){
			$admin_url = env('ADMIN_URL');
			$data['records'] = RoomLog::where('room_id', $id)->filter(function ($data){
				$dateTime = new DateTime("@$data->datetime");
				$data->datetime = $dateTime->format('Y年m月d日 H:i:s');
			})->hidden(['id','room_id','createtime'])->order(['status'=>'desc'])->select();
			$row->trade_log = $data;
			$files = RoomFiles::where('room_id', $id)->filter(function ($data)use($admin_url){
				$data->attachmentAddress = $admin_url.$data->attachmentAddress;
			})->hidden(['id','room_id'])->select();
			$row->files = $files;
		}
		$this->writeJson($row);
	}
	
	#竞拍记录
	function roomLog()
	{
		$id = $this->request->get('id/d');
		$rows = RoomLog::where('room_id', $id)
			->order(['weigh'=>'desc'])
			->paginate()
			->items();
		$this->writeJson($rows);
	}
	
	/**
	 * 收藏
	 */
	function collect()
	{
		$id = $this->request->post('id/d');
		$ret = UserService::collect($id, $this->user_id);
		$this->writeJson($ret);
	}
	
}
